package com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkMoreSpecific

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkTools.LancooMethod
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerAdapter
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerViewHolder
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.TempletGrammarSpecificListInfor
import kotlinx.android.synthetic.main.aibk_adapter_item_specific_grammar.view.*

class AibkMoreSpecificGrammarChildAdapter(
        val activity: AppCompatActivity,
        val templetGrammarSpecificListInfor: TempletGrammarSpecificListInfor,
        val indexA:Int
) : KBaseRecyclerAdapter(){

    override fun getHolderView(parent: ViewGroup?, position: Int): View? {
        return LayoutInflater.from(activity)
                .inflate(R.layout.aibk_adapter_item_specific_grammar, parent, false)
    }

    override fun onBindViewHolder(holder: KBaseRecyclerViewHolder, position: Int) {

        val subTopicName = templetGrammarSpecificListInfor[indexA].SubTopicList[position].SubTopicName
        val isChecked = templetGrammarSpecificListInfor[indexA].SubTopicList[position].IsChecked
        val testLevel = templetGrammarSpecificListInfor[indexA].SubTopicList[position].TestLevel
        val masteryLevel = templetGrammarSpecificListInfor[indexA].SubTopicList[position].MasteryLevel
        val scoreRate = templetGrammarSpecificListInfor[indexA].SubTopicList[position].ScoreRate

        //显示语法名称
        holder.convertView.TvGrammarName.text = subTopicName
        //显示选中状态
        holder.convertView.LlGrammarSpecifiView.isSelected = isChecked
        holder.convertView.BtnAnalysis.isSelected = isChecked
        //显示测试概率
        if(testLevel == 1) {
            holder.convertView.TvTestLevel.text = "常考"
            holder.convertView.TvTestLevel.setTextColor(activity.resources.getColor(R.color.lancoooGreen))
        }else if(testLevel == 2) {
            holder.convertView.TvTestLevel.text = "次常考"
            holder.convertView.TvTestLevel.setTextColor(activity.resources.getColor(R.color.lancoooBlue))
        }else if(testLevel == 3) {
            holder.convertView.TvTestLevel.text = "一般"
            holder.convertView.TvTestLevel.setTextColor(activity.resources.getColor(R.color.lancoooRed))
        }
        //显示分值占比
        val scoreRateX = LancooMethod.getScoreRate(100*scoreRate,4)
        holder.convertView.TvScoreRate.text = scoreRateX
        //显示掌握程度
        if(masteryLevel == 1) {
            holder.convertView.TvMasteryLevel.text = "扎实"
            holder.convertView.TvMasteryLevel.setTextColor(activity.resources.getColor(R.color.lancoooGreen))
        }else if(masteryLevel == 2) {
            holder.convertView.TvMasteryLevel.text = "一般"
            holder.convertView.TvMasteryLevel.setTextColor(activity.resources.getColor(R.color.lancoooBlue))
        }else if(masteryLevel == 3) {
            holder.convertView.TvMasteryLevel.text = "较差"
            holder.convertView.TvMasteryLevel.setTextColor(activity.resources.getColor(R.color.lancoooRed))
        }

        holder.convertView.LlGrammarSpecifiView.setOnClickListener {
            holder.convertView.LlGrammarSpecifiView.isSelected = !holder.convertView.LlGrammarSpecifiView.isSelected
            holder.convertView.BtnAnalysis.isSelected = holder.convertView.LlGrammarSpecifiView.isSelected
            templetGrammarSpecificListInfor[indexA].SubTopicList[position].IsChecked = holder.convertView.LlGrammarSpecifiView.isSelected

            //判断当前分类中小类是否全部被选中（全部选中则【全选】按钮高亮）
            clickListener!!.onClickAllCheck(getIsAllSelected(indexA))
        }
        holder.convertView.BtnAnalysis.setOnClickListener{
//            val dialogSpecificAnalysis = DialogSpecificAnalysis(activity,activity,templetGrammarSpecificListInfor,indexA,position,false)
//            dialogSpecificAnalysis.show()
            clickListener!!.onClickSingleAnalysis(indexA,position)
        }
    }

    override fun getItemCount(): Int {
        return templetGrammarSpecificListInfor[indexA].SubTopicList.size
    }

    //设置专项选择状态回掉，供父级页面执行响应界面反馈（主要用于选中状态的监听）
    interface OnClickListener {
        fun onClickAllCheck(isAllSelected:Boolean)
        fun onClickSingleAnalysis(indexA:Int,indexB:Int)
    }

    private var clickListener: OnClickListener? = null

    fun setClickListener(clickListener: OnClickListener?) {
        this.clickListener = clickListener
    }

    //执行父级adapter点击全选按钮刷新操作
    fun checkAllParent(parentIndex:Int,isSelected: Boolean){
        val childSize = templetGrammarSpecificListInfor[parentIndex].SubTopicList.size
        for(i in 0 until childSize){
            templetGrammarSpecificListInfor[parentIndex].SubTopicList[i].IsChecked = isSelected
        }
        notifyDataSetChanged()
    }

    //return方法：判断当前分类是否为全选状态
    fun getIsAllSelected(parentPosition:Int):Boolean{
        var isAllSelected = true
        val childSize = templetGrammarSpecificListInfor[parentPosition].SubTopicList.size
        outLoop@for(i in 0 until childSize){
            val isSelected = templetGrammarSpecificListInfor[parentPosition].SubTopicList[i].IsChecked
            if(!isSelected){
                isAllSelected = false
                break@outLoop
            }
        }
        return isAllSelected
    }
}